<template>
  <view class="container">
    <!-- 搜索框 -->
    <view class="search-bar">
      <image class="search-icon" src="../../static/icons/search.png"></image>
      <input class="search-input" placeholder="搜索内容" v-model="searchQuery" />
      <button class="search-button" @click="onSearch">| 搜索</button>
    </view>

    <view class="questionnaires-container">
      <view
        class="questionnaire-container"
        v-for="(item, index) in questionnaires"
        :key="index"
        @click="toDetail(item.id)"
      >
        <text class="title">{{ item.title }}</text>
        <view class="source-container">
          <image src="../../static/icons/history.png" class="icon"></image>
          <text class="source">{{ item.source }}</text>
        </view>
        <text class="detail">{{ item.detail }}</text>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useQuestionnaireStore } from '@/store/questionnaire'
const questionnaireStore = useQuestionnaireStore()
const questionnaires = computed(() => questionnaireStore.questionnaires)

const searchQuery = ref('')

onMounted(() => {
  questionnaireStore.clearUserChoices()
})

const toDetail = (questionnaireId: number) => {
  questionnaireStore.setCurrentUQuestionnaire(questionnaireId)
  uni.navigateTo({
    url: '/pages/questionnaire/detail',
  })
}

const onSearch = () => {
  console.log(searchQuery.value)
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  width: 100vw;
  background: linear-gradient(180deg, rgba(215, 229, 247, 1) 0%, rgba(217, 234, 251, 0) 100%);
}

.search-bar {
  display: flex;
  margin-bottom: 16px;
  height: 40px;
  .search-icon {
    width: 25px;
    height: 25px;
    position: absolute;
    left: 20px;
    margin-top: 7px;
    margin-left: 5px;
  }
  .search-input {
    height: 40px;
    flex: 1;
    padding: 8px 40px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 1);
    border: 0.5px solid rgba(0, 117, 254, 1);
  }
  .search-button {
    position: absolute;
    height: 40px;
    line-height: 40px;
    color: #007aff;
    background: none;
    right: 15px;
    font-size: 16px;
    font-weight: bold;
  }
}

.questionnaires-container {
  width: 100%;
  .questionnaire-container {
    margin-bottom: 20px;
    height: 120px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border-radius: 12px;
    padding: 15px;
    background-color: #fff;
    .title {
      font-size: 16px;
      font-weight: bold;
    }
    .source-container {
      margin-top: 5px;
      display: flex;
      .icon {
        width: 20px;
        height: 20px;
        margin-right: 5px;
      }
      .source {
        font-size: 14px;
        color: #8c8c8c;
      }
    }

    .detail {
      overflow: hidden;
      text-overflow: ellipsis;
      width: 100%;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      word-break: break-all;
      font-size: 13px;
      margin-top: 5px;
    }
  }
}
</style>
